package com.atguigu.cmsservice.controller;


import com.atguigu.annotation.WebLog;
import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台 banner 管理接口
 * </p>
 *
 * @author 陈烨庆
 * @since 2022-03-24
 */
@RestController
@RequestMapping("/cmsservice/banner")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @WebLog("分页查询banner")
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable("page") long page, @PathVariable("limit") long limit) {
        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        bannerService.page(pageBanner, null);
        return R.ok().data("items", pageBanner.getRecords()).data("total", pageBanner.getTotal());
    }

    @WebLog("添加banner")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.save(crmBanner);
        return R.ok();
    }

    @WebLog("修改banner")
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.updateById(crmBanner);
        return R.ok();
    }

    @WebLog("查询banner")
    @GetMapping("get/{id}")
    public R get(@RequestBody String id) {
        CrmBanner crmBanner = bannerService.getById(id);
        return R.ok().data("crmBanner", crmBanner);
    }

    @WebLog("删除banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable("id") String id) {
        bannerService.removeById(id);
        return R.ok();
    }

}

